import sys, util.gzopen, sets

class FeatureMap:
    def __init__(self):
        self.features = {}
        self.num_features = 0
        self.labels = {}
        self.rev_labels = {}
        self.num_labels = 0

    def detect(self, feature_string, add_unseen=False):
        if self.features.has_key(feature_string):
            return self.features[feature_string]
        elif add_unseen:
            self.features[feature_string] = self.num_features
            self.num_features += 1
            return self.num_features-1
        return -1

    def detect_tag(self, tag, add_unseen=False):
        if self.labels.has_key(tag):
            return self.labels[tag]
        elif add_unseen:
            self.labels[tag] = self.num_labels
            self.rev_labels[self.num_labels] = tag
            self.num_labels += 1
            return self.num_labels-1
        return -1

    def serialise(self, out_file="feature_map.gz"):
        out = util.gzopen.gzopen(out_file,'w')
        print >>out, len(self.labels)
        for label,id in self.labels.items():
            print >>out, label, id
        for feature,id in self.features.items():
            print >>out, feature, id
        out.close()

    def deserialise(self, in_file="feature_map.gz"):
        input = util.gzopen.gzopen(in_file,'r')
        self.num_labels = int(input.readline())
        counter = 0
        for line in input:
            tag,id = line.split()
            self.labels[tag] = int(id)
            self.rev_labels[int(id)] = tag
            counter+=1
            if counter >= self.num_labels:
                break
        for line in input:
            feature,id = line.split()
            id = int(id)
            self.features[feature] = id
            if id+1 > self.num_features:
                self.num_features = id+1
        input.close()                    

assert len(sys.argv) == 3

# load the feature map
feature_map = FeatureMap()
feature_map.deserialise(sys.argv[1])

tags_in = util.gzopen.gzopen(sys.argv[2],'r')
for line in tags_in:
    tokens = line.split()
    assert len(tokens) <= 1
    if len(tokens) == 0: continue
    print "%d:1.0" % feature_map.detect_tag(tokens[0])
